<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Page
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: AdminLevelController.php 2010-08-31 16:05 idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Page
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Page_AdminLevelController extends Core_Controller_Action_Admin
{
	public function init()
  {
    $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
      ->getNavigation('page_admin_main', array(), 'page_admin_main_level');
  }
  
	public function indexAction()
  {
    if( null !== ($id = $this->_getParam('level_id')) ) {
      $level = Engine_Api::_()->getItem('authorization_level', $id);
    } else {
      $level = Engine_Api::_()->getItemTable('authorization_level')->getDefaultLevel();
    }
    
  	if( !$level instanceof Authorization_Model_Level ) {
      throw new Engine_Exception('missing level');
    }
    
    $id = $level->level_id;
    
    $this->view->form = $form = new Page_Form_Admin_Level();
    $form->level_id->setValue($id);
    
    $permissionsTable = Engine_Api::_()->getDbtable('permissions', 'authorization');
    
    if( !$this->getRequest()->isPost() ) {
      $form->populate($permissionsTable->getAllowed('page', $id, array_keys($form->getValues())));
      return;
    }
    
  	if( !$form->isValid($this->getRequest()->getPost()) ) {
      return;
    }
    
    $values = $form->getValues();
   	// Work around bug :(
   	unset($values['level_id']);
   	
    $db = $permissionsTable->getAdapter();
    $db->beginTransaction();

    try
    {
      // Check license
	  /* Nulled By Hazaraboy
	    $hecoreApi = Engine_Api::_()->getApi('core', 'hecore');
	    $product_result = $hecoreApi->checkProduct('pages');
	    
	    if (isset($product_result['result']) && !$product_result['result']) {
	      $form->addError($product_result['message']);
	      $this->view->headScript()->appendScript($product_result['script']);
	    
	      return;
	    } */

			$values['view'] = 1; // $values['create'];
			$values['comment'] = 1; // $values['create'];
      $permissionsTable->setAllowed('page', $id, $values);
      $db->commit();
    }
    catch( Exception $e )
    {
      $db->rollBack();
      throw $e;
    }
  }
}